package com.franklin.ideaplugin.api.ui;

import com.franklin.ideaplugin.api.utils.SwingUtils;
import com.intellij.openapi.actionSystem.ActionToolbar;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DefaultActionGroup;
import com.intellij.openapi.actionSystem.ex.ActionManagerEx;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;

/**
 * @author Ye Junhui
 * @since 2023/8/10
 */
public interface IBaseToolBarComponent extends IBaseComponent{

    /**
     * 初始化工具栏
     * @param actionGroup
     */
    default void initToolBar(@NotNull DefaultActionGroup actionGroup) {
        ActionToolbar actionToolbar = ActionManagerEx.getInstanceEx().createActionToolbar("TOP", actionGroup, true, true);
        JComponent toolPanel = getToolPanel();
        toolPanel.setLayout(SwingUtils.createLayoutManager(1,1));
        this.addCell(toolPanel,actionToolbar.getComponent(),0,0,new Dimension(-1,-1));
    }

    /**
     * 添加按钮
     * @param actionGroup
     * @param button
     */
    default void addToolButton(@NotNull DefaultActionGroup actionGroup,@NotNull JButton button){
        actionGroup.add(new AnAction(button.getText(), button.getToolTipText(), button.getIcon()) {
            @Override
            public void actionPerformed(@NotNull AnActionEvent e) {
                button.doClick();
            }
        });
    }

    /**
     * 添加按钮
     * @param actionGroup
     * @param anAction
     */
    default void addToolButton(@NotNull DefaultActionGroup actionGroup, @NotNull AnAction anAction){
        actionGroup.add(anAction);
    }

    /**
     * 工具栏
     * @return
     */
    @NotNull
    JComponent getToolPanel();
}
